package com.block.product.service.union;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.common.vo.BaseUserInfoVo;
import com.block.db.entity.ProductInfo;
import com.block.product.service.IUnionProductService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class QiDaiProductServiceImpl implements IUnionProductService {

    private static final String MATCH_USER_URL = "https://www.qdaiapp.com/qidai/matching/user";

    private static final String REGISTER_USER_URL = "https://www.qdaiapp.com/qidai/matching/register";

    @Override
    public ResponseVo matchUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {

        try {

            HttpRequest httpRequest = HttpUtil.createPost(MATCH_USER_URL);
            httpRequest.header("Content-Type", "application/x-www-form-urlencoded");

            String md5Mobile = SecureUtil.md5(userInfoVo.getMobile());
            String timestamp = String.valueOf(System.currentTimeMillis()/1000);

            String sign = productInfo.getChannelCode() + productInfo.getChannelKey() + md5Mobile + timestamp;
            String md5Sign = SecureUtil.md5(sign).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("mobile", md5Mobile);
            paramMap.put("channel", productInfo.getChannelCode());
            paramMap.put("timestamp", timestamp);
            paramMap.put("sign", md5Sign);

            httpRequest.form(paramMap);
            String result = httpRequest.execute().body();

            JSONObject jsonObject = JSON.parseObject(result);
            String status = jsonObject.getString("status");
            if (StrUtil.equals("200", status)){
                return new ResponseVo(AppRspCodeConstant.SUCCESS);
            }

            return new ResponseVo(AppRspCodeConstant.PARAM_ERROR);
        }catch (Exception exception){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }

    @Override
    public ResponseVo registerUserInfo(ProductInfo productInfo, BaseUserInfoVo userInfoVo) {
        try {

            HttpRequest httpRequest = HttpUtil.createPost(REGISTER_USER_URL);
            httpRequest.header("Content-Type", "application/x-www-form-urlencoded");

            String timestamp = String.valueOf(System.currentTimeMillis()/1000);

            String sign = productInfo.getChannelCode() + productInfo.getChannelKey() + userInfoVo.getMobile() + timestamp;
            String md5Sign = SecureUtil.md5(sign).toLowerCase();

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("mobile", userInfoVo.getMobile());
            paramMap.put("channel", productInfo.getChannelCode());
            paramMap.put("timestamp", timestamp);
            paramMap.put("sign", md5Sign);

            httpRequest.form(paramMap);
            String result = httpRequest.execute().body();

            JSONObject jsonObject = JSON.parseObject(result);
            String status = jsonObject.getString("status");
            if (!StrUtil.equals("200", status)){
                return new ResponseVo(AppRspCodeConstant.THRID_FAILED);
            }

            String data = jsonObject.getString("data");
            JSONObject resultData=  new JSONObject();
            resultData.put("productUrl", data);

            return new ResponseVo(AppRspCodeConstant.SUCCESS, resultData);
        }catch (Exception exception){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION);
        }
    }
}
